package com.tcr.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.tcr.common.TcProject;
import com.tcr.common.TcUser;
import com.tcr.dao.ProjectDao;
import com.tcr.dao.ProjectMapDao;
import com.tcr.dao.UserDao;
import com.tcr.util.HashUtil;

public class UserMgtAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public String addUser() {
		String u_name = this.getU_name().trim();
		String u_mail = this.getU_mail().trim();
		String u_pass = this.getU_pass().trim();
		int u_type = Integer.parseInt(this.getU_type().trim());
		int u_upload = Integer.parseInt(this.getU_upload().trim());

		u_pass = HashUtil.getMd5(u_pass);

		boolean b = false;
		b = UserDao.addUser(u_name, u_mail, u_pass, u_type, u_upload);
		if (b) {
			return Action.SUCCESS;
		} else {
			addFieldError("err", "创建用户失败，请重试");
			return Action.INPUT;
		}
	}

	public String usermgt() {
		List<TcUser> user_lists = UserDao.getAllUser();
		this.setUsers(user_lists);
		return Action.SUCCESS;
	}

	public String configUser() {
		int uid = Integer.parseInt(this.getUid().trim());
		TcUser user = UserDao.getUserById(uid);
		this.setUser(user);

		List<TcProject> tc_prjects = ProjectDao.getAllPrjs();
		this.setProjs(tc_prjects);
		serName(tc_prjects);
		return Action.SUCCESS;
	}

	public String deleteUser() {
		int uid = Integer.parseInt(this.getUid());
		UserDao.delUser(uid);
		return Action.SUCCESS;
	}

	public String updateUser() {
		List<TcProject> tc_prjects = ProjectDao.getAllPrjs();
		this.setProjs(tc_prjects);

		String u_pass = this.getU_pass().trim();
		int uid = Integer.parseInt(this.getUid().trim());
		boolean need_pass = true;
		if (this.getU_pass() == null || this.getU_pass().length() == 0)
			need_pass = false;

		u_pass = HashUtil.getMd5(u_pass);
		if (need_pass) {
			boolean b = UserDao.updateUserPass(uid, u_pass);
			if (!b) {
				super.addFieldError("error", "修改密码失败");
				return Action.INPUT;
			}
		}

		int u_upload = Integer.parseInt(this.getU_upload().trim());
		boolean b = UserDao.updateUserUpload(uid, u_upload);
		if (!b) {
			super.addFieldError("error", "修改上传权限失败");
			return Action.INPUT;
		}

		boolean map = true;

		String linkedp = this.getLinkedp();
		if (linkedp == null || linkedp.length() == 0)
			map = false;

		if (map) {
			b = ProjectMapDao.delLinkByUid(uid);

			String[] maps = linkedp.trim().split(",");

			for (String m : maps) {
				int pid = Integer.parseInt(m.trim());
				b = ProjectMapDao.addLinked(uid, pid);
			}
		}

		if (!b) {
			super.addFieldError("error", "添加项目关联失败");
			return Action.INPUT;
		}

		return Action.SUCCESS;
	}

	public void setU_pass(String u_pass) {
		this.u_pass = u_pass;
	}

	public String getU_pass() {
		return u_pass;
	}

	public void setU_name(String u_name) {
		this.u_name = u_name;
	}

	public String getU_name() {
		return u_name;
	}

	public void setU_mail(String u_mail) {
		this.u_mail = u_mail;
	}

	public String getU_mail() {
		return u_mail;
	}

	public void setU_type(String u_type) {
		this.u_type = u_type;
	}

	public String getU_type() {
		return u_type;
	}

	public void setU_upload(String u_upload) {
		this.u_upload = u_upload;
	}

	public String getU_upload() {
		return u_upload;
	}

	public void setUsers(List<TcUser> users) {
		this.users = users;
	}

	public List<TcUser> getUsers() {
		return users;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public String getUid() {
		return uid;
	}

	public void setUser(TcUser user) {
		this.user = user;
	}

	public TcUser getUser() {
		return user;
	}

	public void setProjs(List<TcProject> projs) {
		this.projs = projs;
	}

	public List<TcProject> getProjs() {
		return projs;
	}

	public void setProj_names(List<String> proj_names) {
		this.proj_names = proj_names;
	}

	public List<String> getProj_names() {
		return proj_names;
	}

	// setnames
	private void serName(List<TcProject> projs) {
		List<String> pnames = new ArrayList<String>();
		for (TcProject tp : projs) {
			pnames.add(tp.p_name);
		}
		this.setProj_names(pnames);
	}

	public void setLinkedp(String linkedp) {
		this.linkedp = linkedp;
	}

	public String getLinkedp() {
		return linkedp;
	}

	private String u_name;
	private String u_mail;
	private String u_pass;
	private String u_type;
	private String u_upload;
	private String uid;
	private TcUser user;
	private List<TcUser> users;
	private List<TcProject> projs;
	private List<String> proj_names;
	private String linkedp;

	private final static Logger logger = Logger.getLogger(UserMgtAction.class);
}
